package com.tutorial.expandListView.Adapter;

import java.text.DecimalFormat;
import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseExpandableListAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.tutorial.expandListView.ExpandListChild;
import com.tutorial.expandListView.ExpandListGroup;
import com.waiter.agent.R;

public class ExpandListAdapter extends BaseExpandableListAdapter {

	private Context context;
	private ArrayList<ExpandListGroup> groups; // chua nhung group khac nhau

	public ExpandListAdapter(Context context, ArrayList<ExpandListGroup> groups) {
		this.context = context;
		this.groups = groups;
	}// ham tao dung expand

	public void addItem(ExpandListChild item, ExpandListGroup group) {// them
																		// moi
																		// mot
																		// node
																		// con
																		// vao
																		// trong
																		// 1
																		// group
		if (!groups.contains(group)) {// kiem tra xem expandlist da co group do
										// chua
			groups.add(group);// neu chua co thi them moi group do
		}
		int index = groups.indexOf(group); // tim chi so cua group do
		ArrayList<ExpandListChild> ch = groups.get(index).getItems();// lay cac
																		// node
																		// con
																		// trong
																		// group
																		// (lay
																		// group
																		// do
																		// ra)
		ch.add(item);// them moi
		groups.get(index).setItems(ch);// luu lai su thay doi
	}

	public Object getChild(int groupPosition, int childPosition) {// lay thong
																	// tin node
																	// con trong
																	// 1 group
		// TODO Auto-generated method stub
		ArrayList<ExpandListChild> chList = groups.get(groupPosition)
				.getItems();// lay cac node con trong group co so la
		// groupPosition
		return chList.get(childPosition);// lay node thu childPosition cua group
											// tren
	}

	public long getChildId(int groupPosition, int childPosition) {// lay
																	// childPosition
																	// cua node
																	// con trong
																	// group
		// TODO Auto-generated method stub
		return childPosition;
	}

	public View getChildView(int groupPosition, int childPosition,
			boolean isLastChild, View view, ViewGroup parent) {// lay view cho
																// node con
		ExpandListChild child = (ExpandListChild) getChild(groupPosition,
				childPosition);// lay node con
		if (view == null) {
			LayoutInflater infalInflater = (LayoutInflater) context
					.getSystemService(context.LAYOUT_INFLATER_SERVICE);
			view = infalInflater.inflate(R.layout.expandlist_child_item, null);
		}// neu view chua co thi tao view tu file xml e_c_i
		TextView tv = (TextView) view.findViewById(R.id.tvChild);// lay textView
																	// cua node
																	// con
		tv.setText(child.getName().toString());// gan text name cua node con cho
												// textview
		tv.setTag(child.getTag());// gan text tag cua node con cho textview

		ImageView im = (ImageView) view.findViewById(R.id.imgChild);
		im.setImageResource(R.drawable.burger);

		Button btnPlus = (Button) view.findViewById(R.id.btnChildPlus);
		Button btnMinus = (Button) view.findViewById(R.id.btnChildMinus);
		btnPlus.setOnClickListener(ButtonListener());
		btnMinus.setOnClickListener(ButtonListener());

		return view;
	}

	public OnClickListener ButtonListener() {
		return new OnClickListener() {

			public void onClick(View v) {
				// TODO Auto-generated method stub
				switch (v.getId()) {
				case R.id.btnChildPlus: {
					LinearLayout row = (LinearLayout) v.getParent();
					EditText t = (EditText) row.findViewById(R.id.tvQuantity);
					int _value;
					if (t.getText().toString().compareTo("") == 0)
						_value = 0;
					else
						_value = Integer.parseInt(t.getText().toString());

					_value++;
					DecimalFormat df = new DecimalFormat("#00");
					t.setText(df.format(_value));
					break;
				}

				case R.id.btnChildMinus: {
					LinearLayout row = (LinearLayout) v.getParent();
					EditText t = (EditText) row.findViewById(R.id.tvQuantity);
					int _value;
					if (t.getText().toString().compareTo("") == 0)
						_value = 0;
					else
						_value = Integer.parseInt(t.getText().toString());

					if (_value > 0)
						_value--;
					DecimalFormat df = new DecimalFormat("#00");
					t.setText(df.format(_value));
					break;
				}
				}
			}
		};
	}

	public int getChildrenCount(int groupPosition) {
		// TODO Auto-generated method stub
		ArrayList<ExpandListChild> chList = groups.get(groupPosition)
				.getItems();

		return chList.size();

	}// lay so luong node con cua 1 group co so la groupPosition

	public Object getGroup(int groupPosition) {
		// TODO Auto-generated method stub
		return groups.get(groupPosition);
	}// lay group

	public int getGroupCount() {
		// TODO Auto-generated method stub
		return groups.size();
	}// lay so luong group

	public long getGroupId(int groupPosition) {
		// TODO Auto-generated method stub
		return groupPosition;
	}// lay so cua group

	public View getGroupView(int groupPosition, boolean isLastChild, View view,
			ViewGroup parent) {// lay view cho cac group
		ExpandListGroup group = (ExpandListGroup) getGroup(groupPosition);// lay
																			// group
																			// theo
																			// so
		if (view == null) {
			LayoutInflater inf = (LayoutInflater) context
					.getSystemService(context.LAYOUT_INFLATER_SERVICE);
			view = inf.inflate(R.layout.expandlist_group_item, null);
		}// neu chua cho view thi tao view tu file xml e_g_i
		TextView tv = (TextView) view.findViewById(R.id.tvGroup);// lay tv cua
																	// group
		tv.setText(group.getName());// gan text name cua group cho text view
		// TODO Auto-generated method stub
		return view;
	}

	public boolean hasStableIds() {
		// TODO Auto-generated method stub
		return true;
	}

	public boolean isChildSelectable(int arg0, int arg1) {
		// TODO Auto-generated method stub
		return true;
	}

}